package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ClassName: RoleServiceImpl
 * Description:
 * date: 2019/12/2 16:35
 * Author Airson
 * Since JDK 1.8
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleDao roleDao;
    @Autowired
    MenuDao menuDao;
    @Autowired
    PermissionDao permissionDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //条件查询
        Page<Role> page = roleDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page);
    }

    @Transactional
    public void add(Role role, Integer[] menuIds, Integer[] permissions) {
        //1. 添加角色（主键回显）
        roleDao.add(role);
        //2. 维护角色和菜单的关系
        setMenu(role, menuIds);
        //3.维护橘色和权限的关系
        setPermission(role, permissions);
    }

    @Override
    public void delById(Integer id) {
        //判断是否被用户关联,若关联就不能删除
        Long count = roleDao.findCountById(id);
        if (count > 0) {
            throw new RuntimeException("该角色被用户关联不能被删除!");
        }
        //删除关联菜单
        roleDao.delMenusById(id);
        //删除关联权限
        roleDao.delPermissionsById(id);
        //删除角色
        roleDao.delById(id);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findmenuIdsById(Integer roleId) {
        return roleDao.findmenuIdsByRoleId(roleId);
    }

    @Override
    public List<Integer> findPermissionIdsById(Integer roleId) {
        return roleDao.findPermissionIdsById(roleId);
    }

    @Override
    @Transactional
    public void edit(Role role, Integer[] menuIds, Integer[] permissions) {

        //1. 更新角色
        roleDao.edit(role);
        //2.删除角色和菜单的关系
        menuDao.deleMenuIdById(role.getId());
        //3. 维护角色和菜单的关系
        setMenu(role, menuIds);
        //4.删除角色和权限的关系
        permissionDao.delePermissionIdById(role.getId());
        //5.维护橘色和权限的关系
        setPermission(role, permissions);
    }

    public void setMenu(Role role, Integer[] menuIds) {
        //判空
        if (role.getId() != null && menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                roleDao.setMenu(role.getId(), menuId);
            }
        }
    }

    public void setPermission(Role role, Integer[] permissions) {
        //判空
        if (role.getId() != null && permissions != null && permissions.length > 0) {
            for (Integer permissionId : permissions) {
                roleDao.setPermission(role.getId(), permissionId);
            }
        }
    }


}
